<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组</title>
</head>

<body>
  <script>
    // 1.静态方法
    /* 
      Array.of() 
      为了区别正整数作为参数时，造成歧义，所产生的API
    */

    console.log(Array.of(2))    // [2]
    console.log(new Array(2))   // [空属性 × 2]
    
    /* 
      Array.from() 
      可将类数组或可迭代对象转换为数组
      Array.from(待转换的对象，回调函数（可选），执行回调函数时所绑定的this对象)
    */

    var obj = {0:1, 1:2, 2:3, length:3}
    console.log([].slice.call(obj))
    console.log(Array.from(obj))

    obj = Array.from(obj, function(value, index) {
      return value * 2
    })

    console.log(obj)

    // 2.原型方法
    /* 
       underscore.js lodash.js提供数组相关的辅助函数

       ES9 新增API
       keys()
       values()
       entries()
    */

    var arr = ["a", "b", "c"]
    let key = [...arr.keys()]
    let value = [...arr.values()]
    let entries = [...arr.entries()]

    console.log(key)        // [0, 1, 2]
    console.log(value)      // ['a', 'b', 'c']
    console.log(entries)    // [[0, 'a'], [1, 'b'], [2, 'c']]
  </script>
</body>

</html>